package top.hidoctor.auscultation.widget;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.LinearSmoothScroller;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;

import com.tang.util.DensityUtil;

public class CenterLayoutManager extends LinearLayoutManager {
    static int lastPositon = 0;
    static int targetPosion = 0;

    public CenterLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);

    }

    @Override
    public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
        CenterSmoothScroller smoothScroller = new CenterSmoothScroller(recyclerView.getContext());
        smoothScroller.setTargetPosition(position);
        startSmoothScroll(smoothScroller);
    }

    public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int lastpositon, int position) {
        this.lastPositon = lastpositon;
        this.targetPosion = position;
        smoothScrollToPosition(recyclerView, state, position);
    }

    public static class CenterSmoothScroller extends LinearSmoothScroller {

        private static float duration = 100f;//滚动到指定位置的时间间隔
        private final Context context;

        public CenterSmoothScroller(Context context) {
            super(context);
            this.context = context;
        }
        //360dp
        private static final float MILLISECONDS_PER_INCH = 400f;// 默认为移动一英寸需要花费25ms

        //这里我采用修改calculateSpeedPerPixel方法来改变速度。这里我修改移动一英寸需要花费为10ms，那代表着滚动速度加快了。那么对应的滚动时间就变小了
        @Override
        protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {

//            float newDuration = duration / (Math.abs(targetPosion - lastPositon));//重新计算相近两个位置的滚动间隔
            return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
        }

        @Override
        protected int calculateTimeForScrolling(int dx) {
            return super.calculateTimeForScrolling(dx);
        }
    }
}